clear*

use "${root}/data/processed/final_sample.dta", clear

keep if baseline_sample == 1

* Education outcomes
foreach educ_vars in ratio_StuTea_ps /// 
			avgschool_size_ps ///
			avgclass_size_ps ///
			teachers_100K_ps ///
			schools_100K_ps ///
			childc_estab_100K ///
			presch_estab_100K ///
			test_scores_ps ///
			prog_rate_ps ///
			ideb_ps {
	rename `educ_vars'_t_plus3 `educ_vars'
}
foreach educ_vars in ratio_StuTea_ps /// 
			avgschool_size_ps ///
			avgclass_size_ps ///
			teachers_100K_ps ///
			schools_100K_ps ///
			childc_estab_100K ///
			presch_estab_100K ///
			test_scores_ps ///
			prog_rate_ps ///
			ideb_ps {
					
*** Summary Baseline
	sum `educ_vars' if baseline_sample == 1 & insample_`educ_vars' == 1
	local min_`educ_vars'_1 : di %9.2fc `r(min)'
	local max_`educ_vars'_1 : di %9.2fc `r(max)'
	local mean_`educ_vars'_1 : di %9.2fc `r(mean)'
	local sd_`educ_vars'_1 : di %9.2fc `r(sd)'
	local N_`educ_vars'_1 : di %8.0g `r(N)'

*** Summary Tiebout median
	sum `educ_vars' if baseline_sample == 1 & lame_duck == 1 & insample_`educ_vars' == 1
	local min_`educ_vars'_2 : di %9.2fc `r(min)'
	local max_`educ_vars'_2 : di %9.2fc `r(max)'
	local mean_`educ_vars'_2 : di %9.2fc `r(mean)'
	local sd_`educ_vars'_2 : di %9.2fc `r(sd)'
	local N_`educ_vars'_2 : di %8.0g `r(N)'

*** Summary Tiebout 75th
	sum `educ_vars' if baseline_sample == 1 & tiebout_median_sample == 1 & insample_`educ_vars' == 1
	local min_`educ_vars'_3 : di %9.2fc `r(min)'
	local max_`educ_vars'_3 : di %9.2fc `r(max)'
	local mean_`educ_vars'_3 : di %9.2fc `r(mean)'
	local sd_`educ_vars'_3 : di %9.2fc `r(sd)'
	local N_`educ_vars'_3 : di %8.0g `r(N)'

*** Summary Lame Duck
	sum `educ_vars' if baseline_sample == 1 & coal_dist_median_sample == 1 & insample_`educ_vars' == 1
	local min_`educ_vars'_4 : di %9.2fc `r(min)'
	local max_`educ_vars'_4 : di %9.2fc `r(max)'
	local mean_`educ_vars'_4 : di %9.2fc `r(mean)'
	local sd_`educ_vars'_4 : di %9.2fc `r(sd)'
	local N_`educ_vars'_4 : di %8.0g `r(N)'

*** Summary Oil Windfall
	sum `educ_vars' if baseline_sample == 1 & oil_sample == 1 & insample_`educ_vars' == 1
	local min_`educ_vars'_5 : di %9.2fc `r(min)'
	local max_`educ_vars'_5 : di %9.2fc `r(max)'
	local mean_`educ_vars'_5 : di %9.2fc `r(mean)'
	local sd_`educ_vars'_5 : di %9.2fc `r(sd)'
	local N_`educ_vars'_5 : di %8.0g `r(N)'

}

* Health Outcomes


foreach health_vars in clinic_basic_100K ///
			clinic_total_100K ///
			docs_100K ///
			esf_100K ///
			infant_mort_rate {
	if "`health_vars'"=="esf_100K"{
		gen `health_vars' = `health_vars'_t_plus4
	}
	else {
		gen `health_vars' = exp(`health_vars'_avg/100)
	}
}

foreach health_vars in clinic_basic_100K ///
			clinic_total_100K ///
			docs_100K ///
			esf_100K ///
			infant_mort_rate {
							
*** Summary Baseline
	sum `health_vars' if baseline_sample == 1 & insample_`health_vars' == 1
	local min_`health_vars'_1 : di %9.2fc `r(min)'
	local max_`health_vars'_1 : di %9.2fc `r(max)'
	local mean_`health_vars'_1 : di %9.2fc `r(mean)'
	local sd_`health_vars'_1 : di %9.2fc `r(sd)'
	local N_`health_vars'_1 : di %8.0g `r(N)'

*** Summary Tiebout median
	sum `health_vars' if baseline_sample == 1 & lame_duck == 1 & insample_`health_vars' == 1
	local min_`health_vars'_2 : di %9.2fc `r(min)'
	local max_`health_vars'_2 : di %9.2fc `r(max)'
	local mean_`health_vars'_2 : di %9.2fc `r(mean)'
	local sd_`health_vars'_2 : di %9.2fc `r(sd)'
	local N_`health_vars'_2 : di %8.0g `r(N)'

*** Summary Tiebout 75th
	sum `health_vars' if baseline_sample == 1 & tiebout_median_sample == 1 & insample_`health_vars' == 1
	local min_`health_vars'_3 : di %9.2fc `r(min)'
	local max_`health_vars'_3 : di %9.2fc `r(max)'
	local mean_`health_vars'_3 : di %9.2fc `r(mean)'
	local sd_`health_vars'_3 : di %9.2fc `r(sd)'
	local N_`health_vars'_3 : di %8.0g `r(N)'

*** Summary Lame Duck
	sum `health_vars' if baseline_sample == 1 & coal_dist_median_sample == 1 & insample_`health_vars' == 1
	local min_`health_vars'_4 : di %9.2fc `r(min)'
	local max_`health_vars'_4 : di %9.2fc `r(max)'
	local mean_`health_vars'_4 : di %9.2fc `r(mean)'
	local sd_`health_vars'_4 : di %9.2fc `r(sd)'
	local N_`health_vars'_4 : di %8.0g `r(N)'

*** Summary Oil Windfall
	sum `health_vars' if baseline_sample == 1 & oil_sample == 1 & insample_`health_vars' == 1
	local min_`health_vars'_5 : di %9.2fc `r(min)'
	local max_`health_vars'_5 : di %9.2fc `r(max)'
	local mean_`health_vars'_5 : di %9.2fc `r(mean)'
	local sd_`health_vars'_5 : di %9.2fc `r(sd)'
	local N_`health_vars'_5 : di %8.0g `r(N)'
}


foreach homicides in homicide_rate{
	gen `homicides' = exp(`homicides'_avg/100)
}

foreach homicides in homicide_rate{
							
*** Summary Baseline
	sum `homicides' if baseline_sample == 1 & insample_`homicides' == 1
	local min_`homicides'_1 : di %9.2fc `r(min)'
	local max_`homicides'_1 : di %9.2fc `r(max)'
	local mean_`homicides'_1 : di %9.2fc `r(mean)'
	local sd_`homicides'_1 : di %9.2fc `r(sd)'
	local N_`homicides'_1 : di %8.0g `r(N)'

*** Summary Tiebout median
	sum `homicides' if baseline_sample == 1 & lame_duck == 1 & insample_`homicides' == 1
	local min_`homicides'_2 : di %9.2fc `r(min)'
	local max_`homicides'_2 : di %9.2fc `r(max)'
	local mean_`homicides'_2 : di %9.2fc `r(mean)'
	local sd_`homicides'_2 : di %9.2fc `r(sd)'
	local N_`homicides'_2 : di %8.0g `r(N)'

*** Summary Tiebout 75th
	sum `homicides' if baseline_sample == 1 & tiebout_median_sample == 1 & insample_`homicides' == 1
	local min_`homicides'_3 : di %9.2fc `r(min)'
	local max_`homicides'_3 : di %9.2fc `r(max)'
	local mean_`homicides'_3 : di %9.2fc `r(mean)'
	local sd_`homicides'_3 : di %9.2fc `r(sd)'
	local N_`homicides'_3 : di %8.0g `r(N)'

*** Summary Lame Duck
	sum `homicides' if baseline_sample == 1 & coal_dist_median_sample == 1 & insample_`homicides' == 1
	local min_`homicides'_4 : di %9.2fc `r(min)'
	local max_`homicides'_4 : di %9.2fc `r(max)'
	local mean_`homicides'_4 : di %9.2fc `r(mean)'
	local sd_`homicides'_4 : di %9.2fc `r(sd)'
	local N_`homicides'_4 : di %8.0g `r(N)'

*** Summary Oil Windfall
	sum `homicides' if baseline_sample == 1 & oil_sample == 1 & insample_`homicides' == 1
	local min_`homicides'_5 : di %9.2fc `r(min)'
	local max_`homicides'_5 : di %9.2fc `r(max)'
	local mean_`homicides'_5 : di %9.2fc `r(mean)'
	local sd_`homicides'_5 : di %9.2fc `r(sd)'
	local N_`homicides'_5 : di %8.0g `r(N)'
}

***** LATEX TABLE ****

* write table
texdoc init "${root}/results/tables/summary_stats_outcomes.tex", replace force

tex \caption{Welfare-related outcomes -- descriptive statistics} 
tex \resizebox{\linewidth}{!}{
tex \begin{tabularx}{\linewidth}{l *5{>{\Centering}X}}
tex \toprule

tex 													&        Baseline 				& \multicolumn{4}{c}{Subsamples} 								\\
tex \cmidrule(lr){2-2} \cmidrule(lr){3-6}

tex 													& 								& 	Lame Duck 			& Tiebout $<$ median	 								& Ideology distance $>$ median  						& Oil windfall					\\
tex \midrule
tex \multicolumn{6}{c}{Education outcomes} \\
tex \midrule

tex Student-teachers ratio							&		`mean_ratio_StuTea_ps_1'		&		`mean_ratio_StuTea_ps_2'		&		`mean_ratio_StuTea_ps_3'		&		`mean_ratio_StuTea_ps_4'			&		`mean_ratio_StuTea_ps_5'			\\
tex 							  			&		(`sd_ratio_StuTea_ps_1')		&		(`sd_ratio_StuTea_ps_2')		&		(`sd_ratio_StuTea_ps_3')		&		(`sd_ratio_StuTea_ps_4')			&		(`sd_ratio_StuTea_ps_5')			\\
tex Average classroom size  							&		`mean_avgclass_size_ps_1'		&		`mean_avgclass_size_ps_2'		&		`mean_avgclass_size_ps_3'		&		`mean_avgclass_size_ps_4'			&		`mean_avgclass_size_ps_5'			\\
tex 							  			&		(`sd_avgclass_size_ps_1')		&		(`sd_avgclass_size_ps_2')		&		(`sd_avgclass_size_ps_3')		&		(`sd_avgclass_size_ps_4')			&		(`sd_avgclass_size_ps_5')			\\
tex Teachers, per 100K res.							&		`mean_teachers_100K_ps_1'		&		`mean_teachers_100K_ps_2'		&		`mean_teachers_100K_ps_3'		&		`mean_teachers_100K_ps_4'			&		`mean_teachers_100K_ps_5'			\\
tex 							  			&		(`sd_teachers_100K_ps_1')		&		(`sd_teachers_100K_ps_2')		&		(`sd_teachers_100K_ps_3')		&		(`sd_teachers_100K_ps_4')			&		(`sd_teachers_100K_ps_5')			\\
tex Schools, per 100K res.						&		`mean_schools_100K_ps_1'		&		`mean_schools_100K_ps_2'		&		`mean_schools_100K_ps_3'		&		`mean_schools_100K_ps_4'			&		`mean_schools_100K_ps_5'			\\
tex 							  			&		(`sd_schools_100K_ps_1')		&		(`sd_schools_100K_ps_2')		&		(`sd_schools_100K_ps_3')		&		(`sd_schools_100K_ps_4')			&		(`sd_schools_100K_ps_5')			\\
tex Child Care, per 100K res.						&		`mean_childc_estab_100K_1'		&		`mean_childc_estab_100K_2'		&		`mean_childc_estab_100K_3'		&		`mean_childc_estab_100K_4'			&		`mean_childc_estab_100K_5'			\\
tex 							  			&		(`sd_childc_estab_100K_1')		&		(`sd_childc_estab_100K_2')		&		(`sd_childc_estab_100K_3')		&		(`sd_childc_estab_100K_4')			&		(`sd_childc_estab_100K_5')			\\
tex Prepresch, per 100K res.						&		`mean_presch_estab_100K_1'		&		`mean_presch_estab_100K_2'		&		`mean_presch_estab_100K_3'		&		`mean_presch_estab_100K_4'			&		`mean_presch_estab_100K_5'			\\
tex 							  			&		(`sd_presch_estab_100K_1')		&		(`sd_presch_estab_100K_2')		&		(`sd_presch_estab_100K_3')		&		(`sd_presch_estab_100K_4')			&		(`sd_presch_estab_100K_5')			\\
tex IDEB test scores 								&		`mean_test_scores_ps_1'		&		`mean_test_scores_ps_2'		&		`mean_test_scores_ps_3'		&		`mean_test_scores_ps_4'			&		`mean_test_scores_ps_5'			\\
tex 							  			&		(`sd_test_scores_ps_1')		&		(`sd_test_scores_ps_2')		&		(`sd_test_scores_ps_3')		&		(`sd_test_scores_ps_4')			&		(`sd_test_scores_ps_5')			\\
tex IDEB progression rate  							&		`mean_prog_rate_ps_1'		&		`mean_prog_rate_ps_2'		&		`mean_prog_rate_ps_3'		&		`mean_prog_rate_ps_4'			&		`mean_prog_rate_ps_5'			\\
tex 							  			&		(`sd_prog_rate_ps_1')		&		(`sd_prog_rate_ps_2')		&		(`sd_prog_rate_ps_3')		&		(`sd_prog_rate_ps_4')			&		(`sd_prog_rate_ps_5')			\\
tex IDEB index  								&		`mean_ideb_ps_1'		&		`mean_ideb_ps_2'		&		`mean_ideb_ps_3'		&		`mean_ideb_ps_4'			&		`mean_ideb_ps_5'			\\
tex 							  			&		(`sd_ideb_ps_1')		&		(`sd_ideb_ps_2')		&		(`sd_ideb_ps_3')		&		(`sd_ideb_ps_4')			&		(`sd_ideb_ps_5')			\\

tex \midrule
tex \multicolumn{6}{c}{Health outcomes} \\
tex \midrule

tex Clinics (basic), per 100K res. 							&		`mean_clinic_basic_100K_1'		&		`mean_clinic_basic_100K_2'		&		`mean_clinic_basic_100K_3'		&		`mean_clinic_basic_100K_4'			&		`mean_clinic_basic_100K_5'			\\
tex 							  			&		(`sd_clinic_basic_100K_1')		&		(`sd_clinic_basic_100K_2')		&		(`sd_clinic_basic_100K_3')		&		(`sd_clinic_basic_100K_4')			&		(`sd_clinic_basic_100K_5')			\\
tex Clinics (total), per 100K res.							&		`mean_clinic_total_100K_1'		&		`mean_clinic_total_100K_2'		&		`mean_clinic_total_100K_3'		&		`mean_clinic_total_100K_4'			&		`mean_clinic_total_100K_5'			\\
tex 							  			&		(`sd_clinic_total_100K_1')		&		(`sd_clinic_total_100K_2')		&		(`sd_clinic_total_100K_3')		&		(`sd_clinic_total_100K_4')			&		(`sd_clinic_total_100K_5')			\\

tex ESF team, per 100K res. 								&		`mean_esf_100K_1'		&		`mean_esf_100K_2'		&		`mean_esf_100K_3'		&		`mean_esf_100K_4'			&		`mean_esf_100K_5'			\\
tex 							  			&		(`sd_esf_100K_1')		&		(`sd_esf_100K_2')		&		(`sd_esf_100K_3')		&		(`sd_esf_100K_4')			&		(`sd_esf_100K_5')			\\
tex Doctors, per 100K res.							&		`mean_docs_100K_1'		&		`mean_docs_100K_2'		&		`mean_docs_100K_3'		&		`mean_docs_100K_4'			&		`mean_docs_100K_5'			\\
tex 							  			&		(`sd_docs_100K_1')		&		(`sd_docs_100K_2')		&		(`sd_docs_100K_3')		&		(`sd_docs_100K_4')			&		(`sd_docs_100K_5')			\\
tex Infant mortality rate 								&		`mean_infant_mort_rate_1'		&		`mean_infant_mort_rate_2'		&		`mean_infant_mort_rate_3'		&		`mean_infant_mort_rate_4'			&		`mean_infant_mort_rate_5'			\\
tex 							  			&		(`sd_infant_mort_rate_1')		&		(`sd_infant_mort_rate_2')		&		(`sd_infant_mort_rate_3')		&		(`sd_infant_mort_rate_4')			&		(`sd_infant_mort_rate_5')			\\

tex \midrule
tex \multicolumn{6}{c}{Law enforcement outcomes} \\
tex \midrule

tex Homicide rate 								&		`mean_homicide_rate_1'		&		`mean_homicide_rate_2'		&		`mean_homicide_rate_3'		&		`mean_homicide_rate_4'			&		`mean_homicide_rate_5'			\\
tex 							  			&		(`sd_homicide_rate_1')		&		(`sd_homicide_rate_2')		&		(`sd_homicide_rate_3')		&		(`sd_homicide_rate_4')			&		(`sd_homicide_rate_5')			\\


tex \bottomrule
tex \end{tabularx}}

texdoc close
